using TaxiPlease.ViewModels;
using System.IO.IsolatedStorage;
using System.Windows.Input;
using System;

namespace TaxiPlease
{
	public class OptionsViewModel : ViewModelBase
	{
		public const int SMSLength = 160 - 50; //for the <address>
		private const string defaultPattern = "Can I have a taxi for <address> please!";
		private const string defaultNumber = "18294";

		private const string mapTypeText = "smsPattern";
		private const string patternText = "smsPattern";
		private const string numberText = "taxiPhoneNumber";
		private string smsPattern;
		private string taxiPhoneNumber;
		private bool isLocationEnabled;

		public OptionsViewModel()
		{
			if (!IsolatedStorageSettings.ApplicationSettings.TryGetValue(patternText, out smsPattern))
			{
				smsPattern = defaultPattern;
				IsolatedStorageSettings.ApplicationSettings.Add(patternText, smsPattern);
			}

			if (!IsolatedStorageSettings.ApplicationSettings.TryGetValue(numberText, out taxiPhoneNumber))
			{
				taxiPhoneNumber = defaultNumber;
				IsolatedStorageSettings.ApplicationSettings.Add(numberText, taxiPhoneNumber);
			}

			if (!IsolatedStorageSettings.ApplicationSettings.TryGetValue("IsLocationEnabled", out isLocationEnabled))
			{
				isLocationEnabled = true;
				IsolatedStorageSettings.ApplicationSettings.Add("IsLocationEnabled", isLocationEnabled);
			}
			IsolatedStorageSettings.ApplicationSettings.Save();

			SearchContactsCommand = new SearchContactsCommand(this);
		}

		public ICommand SearchContactsCommand
		{
			get;
			private set;
		}

		public string SmsPattern
		{
			get
			{
				return smsPattern;
			}
			set
			{
				if (smsPattern != value)
				{
					smsPattern = value;
					IsolatedStorageSettings.ApplicationSettings[patternText] = smsPattern;
					IsolatedStorageSettings.ApplicationSettings.Save();
					RaisePropertyChanged("SmsPattern");
					RaisePropertyChanged("RemainingLenght");
				}
			}
		}

		public bool IsLocationEnabled
		{
			get
			{
				return isLocationEnabled;
			}
			set
			{
				if (isLocationEnabled != value)
				{
					isLocationEnabled = value;
					this.RaisePropertyChanged("IsLocationEnabled");
					IsolatedStorageSettings.ApplicationSettings["IsLocationEnabled"] = isLocationEnabled;
					IsolatedStorageSettings.ApplicationSettings.Save();
				}
			}
		}


		public string TaxiPhoneNumber
		{
			get
			{
				return taxiPhoneNumber;
			}
			set
			{
				if (taxiPhoneNumber != value)
				{
					taxiPhoneNumber = value;
					IsolatedStorageSettings.ApplicationSettings[numberText] = taxiPhoneNumber;
					IsolatedStorageSettings.ApplicationSettings.Save();
					this.RaisePropertyChanged("TaxiPhoneNumber");
				}
			}
		}

		public int RemainingLenght
		{
			get
			{
				return SMSLength - this.SmsPattern.Length;
			}
		}
	}
}